<extend name="common/base"/>

<block name="body">
    <table class="table table-bordered datatable"
           data-toggle="datagrid"
           data-options="{
        id:'table',
        checkAction:'backuptabe|立即备份|save|primary,optimize|优化|check|success,repair|修复|wrench|warning',
        width:'100%',
        height:'100%',
        showLinenumber:true,
        selectMult:true,
        showCheckboxcol:true,
        paging:false,
    }"
    style='display:none;'>
        <thead>
            <tr>
                <th data-name="table">表名</th>
                <th data-name="record" width="120">数据量</th>
                <th data-name="size" width="120">数据大小</th>
                <th data-name="createtime" width="160">创建时间</th>
                <th data-name="status" width="160">备份状态</th>
                <th width="120">操作</th>
            </tr>
        </thead>
        <tbody>
            <volist name="list" id="table">
                <tr>
                    <td data-field='table'>{$table.name}</td>
                    <td>{$table.rows}</td>
                    <td>{$table.data_length|format_bytes}</td>
                    <td>{$table.create_time}</td>
                    <td class="info">未备份</td>
                    <td class="action">
                        <a class="ajax-get no-refresh" href="{:url('optimize?tables='.$table['name'])}">优化表</a>&nbsp;
                        <a class="ajax-get no-refresh" href="{:url('repair?tables='.$table['name'])}">修复表</a>
                    </td>
                </tr>
            </volist>
        </tbody>
    </table>
</block>

<block name="script">
    <script type="text/javascript">
        function getSelected(){
            selected = currenttab().find('table.datatable').data('selectedDatas');
            result = [];
            if(selected.length) {
                $.each(selected, function(index,val){
                    if(val.hasOwnProperty('table') && val.table){
                        result.push(val.table);
                    }
                });
            }
            return result;
        }
        function optimize(obj){
            operation(obj, "{:url('optimize')}");
        }
        function repair(obj){
            operation(obj, "{:url('repair')}");
        }
        function operation(obj, url){
            tables = getSelected();
            if(!tables || !tables.length) {
                return false;
            }
            $export = $(obj);
            $export.parent().children().addClass("disabled");
            $.post(url, {tables:tables}, function(data){
                Do.ready('tips', function(){
                    if(data.code){
                        toastr.success(data.msg);
                    } else {
                        toastr.error(data.msg);
                    }
                });
                $export.parent().children().removeClass("disabled");
            }, "json");
            return false;
        }

        function backuptabe(obj) {
            tables = getSelected();
            if(!tables || !tables.length) {
                return false;
            }
            $export = $(obj);
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                '{:url('Database/export')}',
                {tables:tables},
                function(data){
                    if(data.code){
                        tables = data.data.tables;
                        $export.html(data.msg + "开始备份，请不要关闭本页面！");
                        backup(data.data.tab);
                        window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                    } else {
                        Do.ready('tips', function(){
                        	toastr.error(data.msg);
                        });
                        $export.parent().children().removeClass("disabled");
                        $export.html("立即备份");
                        setTimeout(function(){
                            $('#top-alert').find('button').click();
                            $export.removeClass('disabled').prop('disabled',false);
                        },1500);
                    }
                },
                "json"
            );
            return false;
        }

        function backup(tab, status){
            status && showmsg(tab.id, "开始备份...(0%)");
            $.get("{:url('export')}", tab, function(data){
                if(data.code){
                    showmsg(tab.id, data.msg);

                    if(!$.isPlainObject(data.data.tab)){
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function(){ return null }
                        return;
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    Do.ready('tips', function(){
                    	toastr.error(data.msg);
                    });
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
                        $('#top-alert').find('button').click();
                        $(this).removeClass('disabled').prop('disabled',false);
                    },1500);
                }
            }, "json");

        }

        function showmsg(id, msg){
            var $trs = currenttab().find('table.datatable').data('selectedTrs');
            $($trs[id]).find('td.info>div').html(msg);
        }
    </script>
</block>